package com.lishangshan.oas_system.controller.user;

import com.lishangshan.oas_system.model.dao.user.DeptDao;
import com.lishangshan.oas_system.model.dao.user.PositionDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.model.entity.user.Dept;
import com.lishangshan.oas_system.model.entity.user.Position;
import com.lishangshan.oas_system.model.entity.user.User;
import com.lishangshan.oas_system.utils.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 部门管理表 控制器
 */
@Controller
@RequestMapping
public class DeptController {
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private UserDao userDao;

    /**
     * 初始化 部门 管理
     *
     * @param page
     * @return
     */
    // positionmanage
    @RequestMapping("/deptmanage")
    public ModelAndView deptIndex(@RequestParam(value = "page", defaultValue = ParameterUtil.PAGE_NUMBER) int page) {
        Pageable pa = new PageRequest(page, ParameterUtil.PAGE_SIZE_MIN);
        ModelAndView view = new ModelAndView("user/deptmanage");
        List<Dept> depts = deptDao.findAll();
        view.addObject("depts", depts);
        return view;
    }

    /**
     * 跳转部门 修改 或添加 页面
     *
     * @param dept
     * @param model
     * @return
     */
    //    deptedit
    @RequestMapping("/deptedit")
    public String deptedit(@RequestParam(value = "dept", defaultValue = ParameterUtil.ROLE_DEFLUT) Long dept, Model model) {
        if (dept == -1) {
            System.out.println("进入添加部门");
        } else {
            Dept one = deptDao.findOne(dept);
            System.out.println("单查部门信息=" + one);
            model.addAttribute("dept", one);
        }
        return "user/deptedit";
    }

    /**
     * 部门 添加 或 修改
     *
     * @param dept
     * @return
     */
    // deptsave
    @RequestMapping("/deptsave")
    public String deptsave(Dept dept, Model model) {
        if (dept.getDeptId() != null) {
            System.out.println("进入 修改部门");
        }
        Dept save = deptDao.save(dept);
        model.addAttribute("success", true);
        System.out.println("部门 修改 或添加 后的数据 = " + save);
        return "forward:/deptmanage";
    }

    /**
     * 部门删除 功能
     *
     * @param deptid
     * @param model
     * @return
     */
    // deletedept
//    @RequestMapping("/deletedept")
    public String deletedept(@RequestParam("deptid") Long deptid, Model model) {
/*        try {
            deptDao.delete(deptid);
            model.addAttribute("success", true);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        return "forward:/deptmanage";
    }

    @Autowired
    private UserControllerFun userControllerFun;

    // 人事调动 跳转 页面
    //    readdept
    @RequestMapping("/readdept")
    public String readdept(@RequestParam("deptid") Long deptid, Model model) {
        Dept dept = deptDao.findOne(deptid);
        List<User> deptuser = userDao.findByDeptId(deptid);
        if (dept.getDeptmanager() != null) {
            User deptmanage = userDao.findOne(dept.getDeptmanager());
            model.addAttribute("deptmanage", deptmanage);
        }
        // 查询 该部门 是否还有人，如果大于0则有去查询 传值
        if (deptuser.size() > 0) {
            // deptmanage 查询 该部门 经理
            model.addAttribute("deptuser", deptuser);
            model.addAttribute("isread", true);
        }
        // 携下拉 框数据 去页面
        userControllerFun.findSession(model);
        model.addAttribute("dept", dept);
        return "user/deptread";
    }

    /**
     * 进行人事调动
     *
     * @param userid
     * @param deptid
     * @param changedeptid
     * @param positionid
     */
    // deptandpositionchange
    @RequestMapping("/deptandpositionchange")
    public String deptandpositionchange(
            @RequestParam("userid") Long userid, // 调动 用户id
            @RequestParam("deptid") Long deptid, // 改用户处于的部门id
            @RequestParam("changedeptid") Long changedeptid, // 调去哪个部门id
            @RequestParam("positionid") Long positionid) {   // 调去 部门 的哪个职位
        try {
            userDao.updateDept(userid, changedeptid, positionid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 人事调动 初始化
        return "forward:/readdept?deptid=" + deptid;
    }

    /**
     * 删除 部门
     */
    @Autowired
    private PositionDao pdao;

    @RequestMapping("deletedept")
    public String deletdept(@RequestParam("deptid") Long deptid, Model model) {
        // 查找 该部门用户判断是否有用户
        Dept dept = deptDao.findOne(deptid);
        List<Position> ps = pdao.findByDeptid(deptid);
        if (ps.size() > 0) {
            model.addAttribute("deptid", deptid);
            return "forward:/readdept";
        } else {
            deptDao.delete(dept);
            return "forward:/deptmanage";
        }
    }

    /**
     * 更换 部门 经理
     *
     * @param positionid
     * @param changedeptid
     * @param oldmanageid
     * @param newmanageid
     * @param deptid
     * @param model
     * @return
     */
    // deptmanagerchange
    @RequestMapping("deptmanagerchange")
    public String deptmanagerchange(@RequestParam(value = "positionid", required = false) Long positionid,
                                    @RequestParam(value = "changedeptid", required = false) Long changedeptid,
                                    @RequestParam(value = "oldmanageid", required = false) Long oldmanageid,
                                    @RequestParam(value = "newmanageid", required = false) Long newmanageid,
                                    @RequestParam("deptid") Long deptid,
                                    Model model) {
        System.out.println("oldmanageid:" + oldmanageid);
        System.out.println("newmanageid:" + newmanageid);
        Dept deptnow = deptDao.findOne(deptid);
        if (oldmanageid != null) {
            User oldmanage = userDao.findOne(oldmanageid);

            Position namage = oldmanage.getPosition();

            Dept changedept = deptDao.findOne(changedeptid);
            Position changeposition = pdao.findOne(positionid);

            oldmanage.setDept(changedept);
            oldmanage.setPosition(changeposition);
            userDao.save(oldmanage);

            if (newmanageid != null) {
                User newmanage = userDao.findOne(newmanageid);
                newmanage.setPosition(namage);
                deptnow.setDeptmanager(newmanageid);
                deptDao.save(deptnow);
                userDao.save(newmanage);
            } else {
                deptnow.setDeptmanager(null);
                deptDao.save(deptnow);
            }

        } else {
            User newmanage = userDao.findOne(newmanageid);
            Position manage = pdao.findByDeptidAndNameLike(deptid, "%经理").get(0);
            newmanage.setPosition(manage);
            deptnow.setDeptmanager(newmanageid);
            deptDao.save(deptnow);
            userDao.save(newmanage);
        }


        model.addAttribute("deptid", deptid);
        return "/readdept";
    }
}
